package ch.tbz.M326.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.io.File;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.BorderFactory;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.ListCellRenderer;


public class playGUI extends JFrame implements WindowListener  {
	
	//Label
	JLabel titleLbl = new JLabel();
	JLabel timeLbl = new JLabel();
	
	JFrame jf;
	
	//Panel
	JPanel mainPanel = new JPanel();
	JPanel controllPanel = new JPanel();
	JPanel musicPanel = new JPanel();
	JPanel footerPanel = new JPanel();
	JPanel currSongPanel = new JPanel();
	JPanel eastPanel = new JPanel();
	JPanel westPanel = new JPanel();
	JScrollPane musicScrollPane;
	//Buttons
	JButton stoppPlayBtn = new JButton("p");
	JButton stoppPlayBtn1 = new JButton("s");
	JButton prevBtn = new JButton("p");
	JButton nextBtn = new JButton("n");
	JButton pprevBtn = new JButton("pp");
	JButton nnextBtn = new JButton("nn");
	
	//jList
	JList allSongsList = new JList();
	DefaultListModel model = new DefaultListModel();
	
	//TODO
	static final int FPS_MIN = 0;
	static final int FPS_MAX = 10;
	static final int FPS_INIT = 5;  
	JSlider playTimeSldr = new JSlider(JSlider.HORIZONTAL,
	                                      FPS_MIN, FPS_MAX, FPS_INIT);

	private Vector<File> filesVector = null;
	private String[] columnData = new String[]{};
	// Label
	JLabel currSongLbl = new JLabel("Alicia Keys - Ready");
	

	public playGUI(Vector<File> files){
		addWindowListener(this);
		setTitle("MP3-Player");
		filesVector = files;
		
		//TODO

		//[0] name
		//[1] length
		System.out.println("SIZE>> "+filesVector.size());
		for(int i = 0; i < filesVector.size(); i++){
			columnData[i] = filesVector.get(i).getName();
		}
		
		mainPanel.setLayout(new BorderLayout());
		currSongPanel.setLayout(new BorderLayout());
		setLayout(new BorderLayout());
		
		//Slider anpassen
		playTimeSldr.setMajorTickSpacing(10);
		playTimeSldr.setMinorTickSpacing(1);
		playTimeSldr.setPaintTicks(true);
		playTimeSldr.setPaintLabels(true);

		currSongLbl.setHorizontalAlignment(JLabel.CENTER);
		currSongLbl.setVerticalAlignment(JLabel.CENTER);
		
		titleLbl.setText("Titel");
		timeLbl.setText("Dauer");

		//
		
//		allSongsList.setListData(filesVector);
		allSongsList.setCellRenderer(new MyCellRenderer());
		
		musicScrollPane = new JScrollPane(allSongsList);
		
		musicPanel.setSize(200,200);
		
		musicPanel.setLayout(new BorderLayout());
		musicPanel.add(titleLbl, BorderLayout.NORTH);
		musicPanel.add(musicScrollPane, BorderLayout.CENTER);
		
		currSongPanel.add(currSongLbl,BorderLayout.NORTH);
		currSongPanel.add(playTimeSldr,BorderLayout.CENTER);
		controllPanel.add(pprevBtn);
		controllPanel.add(prevBtn);
		controllPanel.add(stoppPlayBtn);
		
		controllPanel.add(currSongPanel);
		controllPanel.add(nextBtn);
		controllPanel.add(nnextBtn);
		controllPanel.add(stoppPlayBtn1);
		
		mainPanel.add(controllPanel, BorderLayout.NORTH);
		mainPanel.add(musicPanel, BorderLayout.CENTER);
		mainPanel.add(footerPanel, BorderLayout.SOUTH);
		mainPanel.add(westPanel, BorderLayout.WEST);
		mainPanel.add(eastPanel, BorderLayout.EAST);
		this.add(mainPanel);
		
	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		// TODO Auto-generated method stub
		System.exit(0);
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	static class MyCellRenderer extends JPanel implements ListCellRenderer{
		JLabel left, middle;
		MyCellRenderer() {
			setLayout(new GridLayout(1, 3));
			left = new JLabel();
			middle = new JLabel();
			left.setOpaque(true);
			middle.setOpaque(true);
			add(left);
			add(middle);
			middle.setHorizontalAlignment(JLabel.RIGHT);
			middle.setBorder(BorderFactory.createLineBorder(Color.black));
			
		}
		@Override
		public Component getListCellRendererComponent(JList list,Object value,int index,boolean isSelected,boolean cellHasFocus){
			String leftData = ((String[])value)[0];
			String middleData = ((String[])value)[1];
			left.setText(leftData);
			middle.setText(middleData);
			if(isSelected){
			left.setBackground(list.getSelectionBackground());
			left.setForeground(list.getSelectionForeground());
			middle.setBackground(list.getSelectionBackground());
			middle.setForeground(list.getSelectionForeground());
			}
			else{
			left.setBackground(list.getBackground());
			left.setForeground(list.getForeground());
			middle.setBackground(list.getBackground());
			middle.setForeground(list.getForeground());
			}
			setEnabled(list.isEnabled());
			setFont(list.getFont());
			return this;
			}
	}
}
